import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router'
// 设置副本
const _axios = axios.create({
        baseURL: process.env.VUE_APP_BASE_API
    })
    //设置请求拦截
_axios.interceptors.request.use(
        config => {
            config.headers.Authorization = `Bearer ${store.state.user.token}`
            return config
        },
        error => {
            return Promise.reject(error)
        }
    )
    //设置响应拦截
_axios.interceptors.response.use(
    res => {
        if (res.data.success) {
            Message.success('登录成功')
            return res.data
        } else {
            Message.error(res.data.message) // 提示错误
            return Promise.reject(res.data.message)
        }
    },
    error => {
        // console.dir(error);
        if (error.response.state === 401) {
            store.commit('user/logout')
            router.push('/login')
            Message.error('登陆失效')
        }
        return Promise.reject(error)
    }
)
export default _axios